package com.vast.admin.controller.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vast.admin.annotation.UseToken;
import com.vast.admin.entity.Company;
import com.vast.admin.entity.Contract;
import com.vast.admin.entity.Customer;
import com.vast.admin.entity.Templete;
import com.vast.admin.entity.dto.ContractDetailDto;
import com.vast.admin.entity.dto.ContractDto;
import com.vast.admin.entity.enums.QueryOrderEnum;
import com.vast.admin.service.*;
import com.vast.admin.service.impl.CompanyService;
import com.vast.common.annotation.Log;
import com.vast.common.core.domain.entity.SysUser;
import com.vast.common.utils.SecurityUtils;
import com.vast.common.utils.StringUtils;
import com.vast.common.constant.MessageCode;
import com.vast.common.core.domain.Result;
import com.vast.system.entity.SuperEntity;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Api(value = "外部对接相关的api", tags = { "外部对接服务模块" })
@RequestMapping("/external")
public class APIController {

    @Autowired
    private ContractService service;

    @Autowired
    private ContractDetailService contractDetailService;

    @Autowired
    private TempleteService templeteService;

    @Autowired
    private PlaceholderService placeholderService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private CompanyService companyService;

    @ApiOperation(value = "作废合同", notes = "作废合同")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "contractId", value = "合同ID", dataType = "Long", required = true)
    })
    @UseToken
    @Log(title = " 作废合同")
    @GetMapping("/cancelContract")
    public Result<Void> cancelContract(Long contractId) {
		SysUser manager = SecurityUtils.getLoginUser().getUser();
        service.cancelContract(contractId, manager);
        return Result.SUCCESS;
    }


    @ApiOperation(value = "查询合同模版列表", notes = "查询合同模版列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token", dataType = "String", required = true),
            @ApiImplicitParam(paramType = "query", name = "size", value = "分页条数", dataType = "int", defaultValue = "10", required = false),
            @ApiImplicitParam(paramType = "query", name = "current", value = "页码,起始值:1", dataType = "int", defaultValue = "1", required = false),
            @ApiImplicitParam(paramType = "query", name = "listMode", value = "是否集合模式:true不进行分页直接返回所有结果集", dataType = "boolean", defaultValue = "false", required = false),
            @ApiImplicitParam(paramType = "query", name = "queryItems", value = "查询条件,json集合", defaultValue = "account,EQ,13419519796;", dataType = "String", required = false),
            @ApiImplicitParam(paramType = "query", name = "orderField", value = "排序字段,多个','分隔", defaultValue = "create_time", dataType = "String",  required = false),
            @ApiImplicitParam(paramType = "query", name = "order", value = "排序类型: ASC/DESC", dataType = "QueryOrderEnum",  required = false)
    })
    @UseToken
    @GetMapping("/getTemplateList")
    public Result<IPage<Templete>> page(@ApiParam(hidden = true) Page<Templete> page, boolean listMode, String queryItems, String orderField, QueryOrderEnum order) {
		SysUser manager = SecurityUtils.getLoginUser().getUser();
    	return new Result<>(MessageCode.SUCCESS, "获取成功", templeteService.queryAppContractTemplateList(manager,page, listMode, queryItems, orderField,order));
    }


    @ApiOperation(value = "根据模版记录id获取模版属性配置字段", notes = "根据模版记录id获取模版属性配置字段")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token", dataType = "String", required = true),
            @ApiImplicitParam(paramType = "query", name = "id", value = "合同记录模版id", dataType = "Long",  required = true)
    })
    @UseToken
    @GetMapping("/getTemplateFieldsById")
    public Result<List<Map<String, Object>>> getTemplateFieldsByTemplateRecordId(Long id) {
        if(id != null) {
            List<Map<String, Object>> templateFields  = placeholderService.getPlaceholderByTempleteRecordId(id);
            if(templateFields != null){
                return new Result(MessageCode.SUCCESS, "获取成功", templateFields);
            }else{
                return new Result(MessageCode.ERROR, "获取失败");
            }
        }else {
            return new Result(MessageCode.PARAM_ERROR, "参数错误");
        }
    }



    @ApiOperation(value = "根据客户手机号码查询客户", notes = "根据客户手机号码查询客户")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token", dataType = "String", required = true),
            @ApiImplicitParam(paramType = "query", name = "type", value = "类型:企业方:1:企业方 2: 客户方", dataType = "int", required = true),
            @ApiImplicitParam(paramType = "query", name = "phone", value = "手机号码", dataType = "String",  required = true)
    })
    @UseToken
    @Log(title = "根据客户手机号码查询客户")
    @GetMapping("/getCustomerByPhone")
    public Result<SuperEntity> getCustomerByPhone(String phone, Integer type) {
        if(!StringUtils.isEmpty(phone) && type != null) {
            if(type == 2){
                Customer customer = customerService.getCustomerByPhone(phone);
                if(customer != null){
                    return new Result(MessageCode.SUCCESS, "获取成功", customer);
                }else{
                    return new Result(MessageCode.SUCCESS, "查无此数据");
                }
            }else if(type == 1 || type == 3 || type ==4){
                Company company =companyService.getCompanyByPhone(phone);
                if(company != null){
                    return new Result(MessageCode.SUCCESS, "获取成功", company);
                }else{
                    return new Result(MessageCode.SUCCESS, "查无此数据");
                }
            }
            return new Result(MessageCode.PARAM_ERROR, "参数错误");
        }else {
            return new Result(MessageCode.PARAM_ERROR, "参数错误");
        }
    }

    @ApiOperation(value = "创建合同签署流程", notes = "创建合同签署流程")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token", dataType = "String", required = true),
            @ApiImplicitParam(paramType = "body", name = "contractDao", value = "合同流程信息", dataType = "ContractDto",  required = true)
    })
    @UseToken
    @Log(title = "创建合同签署流程")
    @PostMapping("/createContract")
    public Result<Long> createContract(@RequestBody ContractDto contractDao) {
        if(contractDao != null) {
			SysUser manager = SecurityUtils.getLoginUser().getUser();
            Long contractId = service.createContract(contractDao,manager);
            if(contractId != null){
                return new Result(MessageCode.SUCCESS, "创建成功", contractId);
            }else{
                return new Result(MessageCode.ERROR, "创建失败");
            }
        }else {
            return new Result(MessageCode.PARAM_ERROR, "参数错误");
        }
    }

    @ApiOperation(value = "添加合同签署流程客户", notes = "添加合同签署流程客户")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token", dataType = "String", required = true),
            @ApiImplicitParam(paramType = "body", name = "contractDao", value = "合同流程信息", dataType = "ContractDto",  required = true)
    })
    @UseToken
    @Log(title = "添加合同签署流程客户")
    @PostMapping("/addContractCustomer")
    public Result<Long> addContractCustomer(@RequestBody ContractDto contractDao) {
        if(contractDao != null && contractDao.getCustomerId() != null) {
			SysUser manager = SecurityUtils.getLoginUser().getUser();
            Long contractId = service.addContractCustomer(contractDao,manager);
            if(contractId != null){
                return new Result(MessageCode.SUCCESS, "添加成功", contractId);
            }else{
                return new Result(MessageCode.ERROR, "添加失败");
            }
        }else {
            return new Result(MessageCode.PARAM_ERROR, "参数错误");
        }
    }


    @ApiOperation(value = "保存合同信息", notes = "保存合同信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token", dataType = "String", required = true),
            @ApiImplicitParam(paramType = "body", name = "contractDetailDto", value = "合同信息", dataType = "ContractDetailDto",  required = true)
    })
    @UseToken
    @Log(title = "保存合同信息")
    @PostMapping("/saveContractDetail")
    public Result<Void> saveContractDetail(@RequestBody ContractDetailDto contractDetailDto) {
        if(contractDetailDto != null) {
			SysUser manager = SecurityUtils.getLoginUser().getUser();
            boolean flag = service.saveContractDetail(contractDetailDto,manager);
            if(flag){
                return new Result(MessageCode.SUCCESS, "保存成功");
            }else{
                return new Result(MessageCode.ERROR, "保存失败");
            }
        }else {
            return new Result(MessageCode.PARAM_ERROR, "参数错误");
        }
    }

    @ApiOperation(value = "获取合同签署流程列表", notes = "获取合同签署流程列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "size", value = "分页条数", dataType = "int", defaultValue = "10", required = false),
            @ApiImplicitParam(paramType = "query", name = "current", value = "页码,起始值:1", dataType = "int", defaultValue = "1", required = false),
            @ApiImplicitParam(paramType = "query", name = "listMode", value = "是否集合模式:true不进行分页直接返回所有结果集", dataType = "boolean", defaultValue = "false", required = false),
            @ApiImplicitParam(paramType = "query", name = "queryItems", value = "查询条件,json集合", defaultValue = "account,EQ,13419519796;", dataType = "String", required = false),
            @ApiImplicitParam(paramType = "query", name = "orderField", value = "排序字段,多个','分隔", defaultValue = "create_time", dataType = "String",  required = false),
            @ApiImplicitParam(paramType = "query", name = "order", value = "排序类型: ASC/DESC", dataType = "QueryOrderEnum",  required = false)
    })
    @UseToken
    @GetMapping("/getContractList")
    public Result<IPage<Contract>> getContractList(@ApiParam(hidden = true) Page<Contract> page, boolean listMode, String queryItems, String orderField, QueryOrderEnum order) {
		SysUser manager = SecurityUtils.getLoginUser().getUser();
        return new Result<>(MessageCode.SUCCESS, "获取成功", service.queryList(manager,page, listMode, queryItems, orderField,order));
    }


    @ApiOperation(value = "锁定并生成合同", notes = "锁定并生成合同")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "合同流程ID", dataType = "Long", required = true),
            @ApiImplicitParam(paramType = "query", name = "flag", value = "是否检查签署者（0检查 1忽略）", dataType = "Integer", required = false)
    })
    @UseToken
    @Log(title = "锁定并生成合同")
    @GetMapping("/lockAndProduceContract")
    public Result<Void> lockAndProduceContract(Long id, @RequestParam(defaultValue = "0") Integer flag){
        if(id != null && id > 0) {
			SysUser manager = SecurityUtils.getLoginUser().getUser();
            service.productionContract(id, manager, flag);
            return new Result(MessageCode.SUCCESS, "操作成功");
        }else {
            return new Result(MessageCode.PARAM_ERROR, "参数错误");
        }
    }



    @ApiOperation(value = "获取合同签署流程里的合同详情", notes = "获取合同签署流程里的合同详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "contractId", value = "合同ID", dataType = "Long", required = true)
    })
    @UseToken
    @GetMapping("/getContractDetailById")
    public Result<ContractDetailDto> getContractDetailById(Long contractId){
        if(contractId != null && contractId > 0) {
            ContractDetailDto result = service.getContractDetail(contractId);
            return new Result(MessageCode.SUCCESS, "获取成功", result);
        }else {
            return new Result(MessageCode.PARAM_ERROR, "参数错误");
        }
    }



}
